package com.example.bookkeeping1220;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.bookkeeping1220.adapter.AccountAdapter;
import com.example.bookkeeping1220.adapter.AchievementAdapter;
import com.example.bookkeeping1220.adapter.CharactersAdapter;
import com.example.bookkeeping1220.db.AccountBean;
import com.example.bookkeeping1220.db.AchievementBean;
import com.example.bookkeeping1220.db.CharactersBean;
import com.example.bookkeeping1220.db.DBManager;
import com.example.bookkeeping1220.talk.TalkActivity1;
import com.example.bookkeeping1220.talk.TalkActivity2;
import com.example.bookkeeping1220.talk.TalkActivity3;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import org.w3c.dom.CharacterData;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class CharactersActivity extends AppCompatActivity {
    ListView characterLv;
    CharactersAdapter charactersAdapter;
    List<CharactersBean> charactersDatas;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_characters);
        characterLv = findViewById(R.id.characters_lv);
        charactersDatas = new ArrayList<>();
        buildCharacter();
        charactersAdapter = new CharactersAdapter(CharactersActivity.this, charactersDatas);
        characterLv.setAdapter(charactersAdapter);
        setLVonClickListener();


        // ZWX
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);

        bottomNavigationView.setSelectedItemId(R.id.chat);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuitem) {
                switch (menuitem.getItemId()) {
                    case R.id.bill:
                        startActivity(new Intent(getApplicationContext(), MainActivity.class));
                        overridePendingTransition(0, 0);
                        return true;

                    case R.id.chat:
                        overridePendingTransition(0, 0);
                        return true;

                    case R.id.person:
                        startActivity(new Intent(getApplicationContext(), UserActivity.class));
                        overridePendingTransition(0, 0);
                        return true;
                }
                return false;
            }
        });
    }

    public void onClick(View view) {
    }

    private void setLVonClickListener() {
        characterLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CharactersBean clickBean = charactersDatas.get(position);  //获取正在被点击的这条信息

                showScoreDialog(clickBean);

            }
        });
    }

    private void showScoreDialog(CharactersBean clickBean) {
        if (clickBean.getIsUnlocked() == 1) {
            int myid = clickBean.getId();
            if (myid == 1) {
                Intent it = new Intent(this, TalkActivity1.class);
                startActivity(it);
            } else if (myid == 2) {
                Intent it = new Intent(this, TalkActivity2.class);
                startActivity(it);
            } else if (myid == 3) {
                Intent it = new Intent(this, TalkActivity3.class);
                startActivity(it);
            }
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示信息").setMessage("当前角色未解锁，你有积分" + DBManager.getUserScore() + ",所需积分" + clickBean.getRequireScore())
                    .setNegativeButton("取消", null)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (DBManager.getUserScore() > clickBean.getRequireScore()) {
                                int resultScore = DBManager.getUserScore() - clickBean.getRequireScore();
                                charactersDatas.get(clickBean.getId() - 1).setIsUnlocked(1);
                                DBManager.renewVirtualCharacterStateFromVirtualCharactertb(charactersDatas);
                                DBManager.setScore(resultScore);
                            } else {
                                Toast toastCenter = Toast.makeText(CharactersActivity.this, "当前积分不足", Toast.LENGTH_SHORT);
                                toastCenter.setGravity(Gravity.CENTER, 0, 0);
                                toastCenter.show();
                            }
                        }
                    });
            builder.create().show();   //显示对话框
        }
    }

    void buildCharacter() {
        charactersDatas.clear();
        List<CharactersBean> list = DBManager.getVirtualCharacterFromVirtualCharactertb();
        charactersDatas.addAll(list);
    }

    @Override
    protected void onResume() {
        super.onResume();
        buildCharacter();
    }
}
